<?php
/**
 * create to 2021/6/24 19:35
 */

namespace app\mainadmin\controller;

use app\mainadmin\model\SettingsModel;
use app\mainadmin\model\LiveStatisticsModel;
use app\AdminController;

/**
 * 直播配置
 * Class Index
 * @package app\store\controller
 */
class Liveing extends AdminController
{

    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize(){
        parent::initialize();
        $this->Model = new SettingsModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index() {

        $setting = $this->Model->where(array('name'=>'live_entrance_setting'))->find();
        $arr = json_decode($setting['data'],true);
        $this->assign('setting',$arr);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 保存配置
    /*------------------------------------------------------ */
    public function save(){
        $set = input('post.');
        if (empty($set['live_time'])){
            return $this->error('请选择开播时间');
        }
        $save_data = json_encode($set);
        if (!$this->Model->where(array('name'=>'live_entrance_setting'))->find()) {
            $insert_data = [
                'data'=>$save_data,
                'is_open'=>0,
                'name'=>'live_entrance_setting'
            ];
            $res = $this->Model->insert($insert_data);
        } else {
            $res = $this->Model->where('name','live_entrance_setting')->update(['data'=>$save_data]);
        }
        if ($res == false) return $this->error();
        return $this->success('设置成功.');
    }

    /*------------------------------------------------------ */
    //-- 屏蔽敏感词
    /*------------------------------------------------------ */
    public function sensitivewords() {

        $setting = $this->Model->where(array('name'=>'live_sensitivewords_setting'))->find();
        $this->assign('setting',$setting);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 保存敏感词配置
    /*------------------------------------------------------ */
    public function saveSensitive() {
        $set = input('post.');
        if (!$this->Model->where(array('name'=>'live_sensitivewords_setting'))->find()) {
            $insert_data = [
                'data'=>$set['data'],
                'is_open'=>0,
                'name'=>'live_sensitivewords_setting'
            ];
            $res = $this->Model->insert($insert_data);
        } else {
            $res = $this->Model->where('name','live_sensitivewords_setting')->update(['data'=>$set['data']]);
        }
        if ($res == false) return $this->error();
        return $this->success('设置成功.');
    }

    /*------------------------------------------------------ */
    //-- 直播数据
    /*------------------------------------------------------ */
    public function statistics() {
        $statisticsModel = new LiveStatisticsModel();
        $now_day = date("Y-m-d",time());
        $seven_days = [];
        for ($i=0;$i<7;$i++) {
            $seven_days[] = date("Y-m-d",strtotime("-{$i} day"));
        }
        $today_pay_money = $statisticsModel->where('day',date("Y-m-d",time()))->sum('pay_money');
        $all_data = [
            'all_live_num'=>$statisticsModel->count(),
            'all_live_money'=>$statisticsModel->sum('pay_money'),
            'today_user_nums'=>$statisticsModel->where('day',date("Y-m-d",time()))->sum('user_nums'),
            'today_order_nums'=>$statisticsModel->where('day',date("Y-m-d",time()))->sum('order_nums'),
            'today_pay_money'=>$today_pay_money,
            'today_user_pay_nums'=>$statisticsModel->where('day',date("Y-m-d",time()))->sum('user_pay_nums'),
        ];
        sort($seven_days);
        $seven_days_data = [];
        for ($i=0;$i<7;$i++) {
            $seven_days_data[$i][] = $statisticsModel->where('day',$seven_days[$i])->sum('pay_money');
        }
        $this->assign('start_day',$seven_days[0]);
        $this->assign('end_day',$seven_days[6]);
        $this->assign('seven_days',json_encode($seven_days));
        $this->assign('seven_days_data',json_encode($seven_days_data));
        $this->assign('all_data',$all_data);
        return $this->fetch();
    }
}